//
//  UISegmentedControlViewController.m
//  UITest
//
//  Created by wp w on 12-6-9.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UISegmentedControlViewController.h"

@interface UISegmentedControlViewController ()

@end

@implementation UISegmentedControlViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)segmentClick:(id)sender
{
	NSLog(@"you selected segment %d",[sender selectedSegmentIndex]);
    NSLog(@"you selected segment title %@",[sender titleForSegmentAtIndex:[sender selectedSegmentIndex]]);
}

- (void)viewDidLoad {
	
    [super viewDidLoad];
	
	NSArray *arrSegments = [[NSArray alloc] initWithObjects:
							[NSString stringWithString:@"0"],
							[NSString stringWithString:@"1"],
							[NSString stringWithString:@"2"],nil];
	
    //新建分段控件
	UISegmentedControl *mySegment = [[UISegmentedControl alloc] initWithItems:arrSegments];
	
	CGRect segmentRect = CGRectMake(10,50,300,40);
	[mySegment setFrame:segmentRect];
    
    //选中状态，是否时鼠标离开后消失
    mySegment.momentary = YES;   
	
    //当点击时，响应事件
	[mySegment addTarget:self action:@selector(segmentClick:) forControlEvents:UIControlEventValueChanged];
	
    //设置style和color
	[mySegment setSegmentedControlStyle:UISegmentedControlStyleBar];
	[mySegment setTintColor:[UIColor darkGrayColor]];
	
	//select first item
	[mySegment setSelectedSegmentIndex:2];
	
	//change a segment size
	[mySegment setWidth:120.0 forSegmentAtIndex:1];
	
	//add a new segment
    //还可以insert img
	[mySegment insertSegmentWithTitle:@"new" atIndex:2 animated:YES];
    [mySegment insertSegmentWithTitle:@"new2" atIndex:3 animated:YES];
    
    //delete a segment
    [mySegment removeSegmentAtIndex:2 animated:YES];
    
    //设置某个index不可用
    [mySegment setEnabled:NO forSegmentAtIndex:3];
    
	//add segment to main view
	[self.view addSubview:mySegment];	
	
	[arrSegments release];
	[mySegment release];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
